﻿using System;
using System.Collections.Generic;
using System.Text;
using FeatureCenter.Module.InfoPanels;
using DevExpress.ExpressApp.Templates;
using DevExpress.XtraEditors;
using FeatureCenter.Module.Win.Templates;
using FeatureCenter.Module.Win;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.Persistent.Base;
using FeatureCenter.Module;

namespace DevExpress.ExpressApp.Demos {
    public class InfoPanelsViewController : InfoPanelsViewControllerBase<IInfoPanelTemplate> {
        private GroupControl textPanel;
        private GroupControl actionConainersPanel;
        private GroupControl relatedViewPanel;
        private LabelControl literal;
        private void UpdateInfoPanelVisibility(SplitContainerControl splitContainer) {
            splitContainer.PanelVisibility = splitContainer.Panel2.Controls.Count > 0 ? SplitPanelVisibility.Both : SplitPanelVisibility.Panel1;
        }
        private void EnsureControls() {
            if(textPanel == null) {
                CreateTextPanel();
                CreateActionContainerPanel();
                CreateRelatedViewPanel();
            }
        }
        private void CreateRelatedViewPanel() {
            relatedViewPanel = CreateGroupControl(Hints.FrameTemplates_InfoPanels_ContextualView);
            System.Windows.Forms.Control viewControl = (System.Windows.Forms.Control)CreateRelatedViewControl();
            viewControl.Dock = System.Windows.Forms.DockStyle.Top;
            relatedViewPanel.Controls.Add(viewControl);
        }
        private void CreateActionContainerPanel() {
            actionConainersPanel = CreateGroupControl(Hints.FrameTemplates_InfoPanels_ContextualActions);
            ButtonsContainer actionContainer = new ButtonsContainer();
            actionContainer.Dock = System.Windows.Forms.DockStyle.Top;
            actionContainer.ContainerId = "ContextActions";
            actionConainersPanel.Controls.Add(actionContainer);
            IDynamicContainersTemplate template = Frame.Template as IDynamicContainersTemplate;
            if(template != null) {
                template.RegisterActionContainers(new[] { actionContainer });
            }
        }
        private void CreateTextPanel() {
            textPanel = CreateGroupControl(Hints.FrameTemplates_InfoPanels_ContextualInfo);
            textPanel.Padding = new System.Windows.Forms.Padding(6);
            literal = new LabelControl();
            literal.AllowHtmlString = true;
            literal.Dock = System.Windows.Forms.DockStyle.Top;
            literal.AutoSizeMode = LabelAutoSizeMode.Vertical;
            textPanel.Controls.Add(literal);
        }
        private GroupControl CreateGroupControl(string caption) {
            GroupControl panel = new GroupControl();
            panel.AutoSize = true;
            panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            panel.Dock = System.Windows.Forms.DockStyle.Top;
            panel.Text = caption;
            return panel;
        }
        protected override void SetInfoTextToControls(string text) {
            if(literal != null) {
                literal.Text = text;
            }
        }
        protected override void AddControlsToTemplateCore(IInfoPanelTemplate template) {
            EnsureControls();
            template.SplitContainer.Panel2.Controls.Add(relatedViewPanel);
            template.SplitContainer.Panel2.Controls.Add(actionConainersPanel);
            template.SplitContainer.Panel2.Controls.Add(textPanel);
            UpdateInfoPanelVisibility(template.SplitContainer);
        }
        protected override void RemoveControlsFromTemplateCore(IInfoPanelTemplate template) {
            template.SplitContainer.Panel2.Controls.Remove(relatedViewPanel);
            template.SplitContainer.Panel2.Controls.Remove(actionConainersPanel);
            template.SplitContainer.Panel2.Controls.Remove(textPanel);
            UpdateInfoPanelVisibility(template.SplitContainer);
            textPanel = null;
            actionConainersPanel = null;
            literal = null;
        }
    }
}
